package com.hz.monitoring.module.hardware.service.monitoring_warning_log;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log.vo.MonitoringWarningLogCreateReqVO;
import com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log.vo.MonitoringWarningLogExportReqVO;
import com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log.vo.MonitoringWarningLogPageReqVO;
import com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log.vo.MonitoringWarningLogUpdateReqVO;
import com.hz.monitoring.module.hardware.convert.monitoring_warning_log.MonitoringWarningLogConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.monitoring_warning_log.MonitoringWarningLogDO;
import com.hz.monitoring.module.hardware.dal.mysql.monitoring_warning_log.MonitoringWarningLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.MONITORING_WARNING_LOG_NOT_EXISTS;

/**
 * 预警日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MonitoringWarningLogServiceImpl implements MonitoringWarningLogService {

    @Resource
    private MonitoringWarningLogMapper monitoringWarningLogMapper;

    @Override
    public Long createMonitoringWarningLog(MonitoringWarningLogCreateReqVO createReqVO) {
        // 插入
        MonitoringWarningLogDO monitoringWarningLog = MonitoringWarningLogConvert.convert(createReqVO);
        monitoringWarningLogMapper.insert(monitoringWarningLog);
        // 返回
        return monitoringWarningLog.getId();
    }

    @Override
    public Boolean createBatchMonitoringWarningLog(List<MonitoringWarningLogCreateReqVO> cmd) {
        List<MonitoringWarningLogDO> monitoringWarningLogDOList = cmd.stream().map(MonitoringWarningLogConvert::convert).collect(Collectors.toList());
        monitoringWarningLogMapper.insertBatch(monitoringWarningLogDOList);
        return true;
    }

    @Override
    public void updateMonitoringWarningLog(MonitoringWarningLogUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMonitoringWarningLogExists(updateReqVO.getId());
        // 更新
        MonitoringWarningLogDO updateObj = MonitoringWarningLogConvert.convert(updateReqVO);
        monitoringWarningLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMonitoringWarningLog(Long id) {
        // 校验存在
        this.validateMonitoringWarningLogExists(id);
        // 删除
        monitoringWarningLogMapper.deleteById(id);
    }

    private void validateMonitoringWarningLogExists(Long id) {
        if (monitoringWarningLogMapper.selectById(id) == null) {
            throw exception(MONITORING_WARNING_LOG_NOT_EXISTS);
        }
    }

    @Override
    public MonitoringWarningLogDO getMonitoringWarningLog(Long id) {
        return monitoringWarningLogMapper.selectById(id);
    }

    @Override
    public List<MonitoringWarningLogDO> getMonitoringWarningLogList(Collection<Long> ids) {
        return monitoringWarningLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MonitoringWarningLogDO> getMonitoringWarningLogPage(MonitoringWarningLogPageReqVO pageReqVO) {
        return monitoringWarningLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MonitoringWarningLogDO> getMonitoringWarningLogList(MonitoringWarningLogExportReqVO exportReqVO) {
        return monitoringWarningLogMapper.selectList(exportReqVO);
    }

}
